package com.chuangxin.data.core;

import com.chuangxin.data.core.io.DataOutput;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Dawnwords on 2015/2/2.
 */
public abstract class WebPageFetcher {
    private static final String ZH_PATTERN = "[\\u4e00-\\u9fa5]+";
    private static final int CAPACITY = 20;
    private BlockingQueue<String> urlQueue;
    private HashMap<String, Integer> urlReappearTime;
    private DataOutput output;
    private int threadNum;
    private int timeout;
    private double sleepSecond;

    public WebPageFetcher(DataOutput output) {
        this(1, output);
    }

    public WebPageFetcher(double sleepSecond, DataOutput output) {
        this(10, 1, sleepSecond, output);
    }

    public WebPageFetcher(int timeout, int threadNum, double sleepSecond, DataOutput output) {
        this.timeout = timeout;
        this.threadNum = threadNum;
        this.output = output;
        this.sleepSecond = sleepSecond;
        this.urlReappearTime = new HashMap<String, Integer>();
        this.urlQueue = new LinkedBlockingQueue<String>(CAPACITY);
    }

    public void execute() {
        for (int i = 0; i < threadNum; i++) {
            new URLProcessor().start();
        }
    }

    protected void putURL(String nextURL) {
        try {
            urlQueue.put(nextURL);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    protected String pollURL() {
        String result = null;
        try {
            result = urlQueue.poll(timeout, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }

    protected void failURL(String reason, final String url) {
        Integer times = urlReappearTime.get(url);
        if (times == null) {
            times = 0;
        }
        urlReappearTime.put(url, times + 1);

        if (times < 3) {
            new Thread() {
                @Override
                public void run() {
                    try {
                        urlQueue.put(url);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        } else {
            Logger.fail(reason, url);
        }
    }

    protected void outputResult(TitleContent tc) {
        output.output(tc.title, tc.content);
    }

    protected abstract TitleContent processDoc(Document doc, String url);

    protected class TitleContent {
        public final String title, content;

        public TitleContent(String title, String content) {
            this.title = title;
            this.content = content;
        }
    }

    protected class URLProcessor extends Thread {
        @Override
        public void run() {
            String url;
            while ((url = pollURL()) != null) {
                process(url);
                try {
                    sleep((long) (sleepSecond * 1000));
                } catch (InterruptedException ignored) {
                }
            }

        }

        private void process(final String url) {
            Document doc;
            try {
                doc = Jsoup.connect(encodeUTF8(url)).get();
            } catch (IOException e) {
                failURL("Timeout", url);
                return;
            }

            TitleContent tc = processDoc(doc, url);
            if (tc != null) {
                if (tc.title == null || "".equals(tc.title)) {
                    failURL("Parse", url);
                } else {
                    Logger.succeed(String.format("%s:%s", url, tc.title));
                    outputResult(tc);
                }
            } else {
                Logger.succeed(url);
            }
        }
    }

    private String encodeUTF8(String str) throws UnsupportedEncodingException {
        Pattern p = Pattern.compile(ZH_PATTERN);
        Matcher m = p.matcher(str);
        StringBuffer b = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(b, URLEncoder.encode(m.group(0), "UTF-8"));
        }
        m.appendTail(b);
        return b.toString();
    }
}
